﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Xml;

namespace Daneas.Utility.Universals
{
    /// <summary>
    /// 发送邮件，可以发单人，发多人，抄送，密送，发送附件
    /// </summary>
    public class MailSender
    {
        /// <summary>
        /// 通过SMTP服务器发送邮件
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static bool SendMailSMTP(MailParam param)
        {
            MailMessage msg = InitialMail(param);

            if (msg == null)
            {
                return false;
            }

            SmtpClient client = new SmtpClient();

            client.Credentials = new System.Net.NetworkCredential(param.FromMail, param.MailPwd);
            client.Port = param.Port;
            client.Host = param.STMPServer;
            client.EnableSsl = param.EnableSsl;


            object userState = msg;
            try
            {
                //client.SendAsync(msg, userState);
                //简单一点儿可以
                client.Send(msg);
                return true;
            }
            catch 
            {
                throw new Exception();
            }
            finally
            {
                client.Dispose();
            }
        }

        /// <summary>
        /// 本地发送邮件
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static bool SendMailLocalhost(MailParam param)
        {
            MailMessage msg = InitialMail(param);

            SmtpClient client = new SmtpClient();
            client.Host = "localhost";
            object userState = msg;

            try
            {
                //client.SendAsync(msg, userState);
                //简单一点儿可以
                client.Send(msg);
                return true;
            }
            catch 
            {
                throw new Exception();
            }
            finally
            {
                client.Dispose();
            }
        }

        /// <summary>
        /// 构建邮件内容
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static MailMessage InitialMail(MailParam param)
        {
            if (param.FromMail.Contains("gmail.com"))
            {
                param.Port = 465;
                param.STMPServer = "smtp.gmail.com";
            }

            else if (param.FromMail.Contains("163.com"))
            {
                param.STMPServer = "smtp.163.com";
                param.Port = 25;
            }

            else if (param.FromMail.Contains("126.com"))
            {
                param.STMPServer = "smtp.126.com";
                param.Port = 785;
            }

            else if (param.FromMail.Contains("qq.com"))
            {
                param.STMPServer = "smtp.qq.com";
                param.Port = 25;
            }

            MailMessage msg = new MailMessage();

            if (param.To == null || param.To.Count <= 0)
            {
                return null;
            }

            //可发送多人
            foreach (var item in param.To)
            {
                MailAddress address = new MailAddress(item.Value, item.Key == null ? item.Value : item.Key);
                msg.To.Add(address);
            }

            //可抄送多人,密送
            if (param.CopyTo != null)
            {
                foreach (var item in param.CopyTo)
                {
                    MailAddress address = new MailAddress(item.Value, item.Key);
                    msg.Bcc.Add(address);
                }
            }


            //3个参数分别是发件人地址（可以随便写），发件人姓名，编码*/ 
            msg.From = new MailAddress(param.FromMail, param.FromDisplay == null ? param.FromMail : param.FromDisplay, System.Text.Encoding.UTF8);
            msg.Subject = param.Subject;//邮件标题 
            msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码 
            msg.Body = param.Body;//邮件内容 
            msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码 
            msg.IsBodyHtml = param.IsBodyHtml;//是否是HTML邮件 
            msg.Priority = MailPriority.High;//邮件优先级 


            return msg;
        }
    }

    public class MailParam
    {
        public MailParam()
        {
            this.IsBodyHtml = true;

            this.EnableSsl = true;
        }

        /// <summary>
        /// Key记录名称，Value记录邮箱地址
        /// </summary>
        public Dictionary<string, string> To { get; set; }

        /// <summary>
        /// Key记录名称，Value记录邮箱地址
        /// </summary>
        public Dictionary<string, string> CopyTo { get; set; }

        /// <summary>
        /// Key记录名称，Value记录文件
        /// </summary>
        public Dictionary<string, string> Files { get; set; }

        /// <summary>
        /// 发送邮箱
        /// </summary>
        public string FromMail { get; set; }

        /// <summary>
        /// 发送人显示名称
        /// </summary>
        public string FromDisplay { get; set; }

        /// <summary>
        /// 发送邮箱密码
        /// </summary>
        public string MailPwd { get; set; }

        /// <summary>
        /// 发送的stmp
        /// </summary>
        public string STMPServer { get; set; }

        /// <summary>
        /// 发送邮件端口号
        /// </summary>
        public int Port { get; set; }

        /// <summary>
        /// 邮件主题
        /// </summary>
        public string Subject { get; set; }

        /// <summary>
        /// 邮件内容
        /// </summary>
        public string Body { get; set; }

        /// <summary>
        /// 是否HTML邮件
        /// </summary>
        public bool IsBodyHtml { get; set; }

        /// <summary>
        /// 是否ssl加密
        /// </summary>
        public bool EnableSsl { get; set; }
    }
}
